package com.newbsd.web.maventest.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.newbsd.web.maventest.pojo.User;
import com.newbsd.web.maventest.util.Md5Utils;
/**
 * 
 * @author 张成玉 DAO层
 *
 */
public class LoginDao {
    /**
     * @author 张成玉 -向数据库中查询一条数据
     * @param username
     * @param password
     * @return
     * @throws SQLException
     */
	public List<User> login(String username,String password) throws SQLException{
		Connection conn = ConnectDb.Connect();
		QueryRunner runner = new QueryRunner();
		List<User> list = runner.query(conn, "select * from user where username=? and password=?",  new BeanListHandler<User>(User.class),username,Md5Utils.md5(password));
		return list;
		
	}
	 /**
     * @author 张成玉 -向数据库中插入一条数据
     * @param username
     * @param password
     * @return
     * @throws SQLException
     */
	public User registUser(User user)throws Exception{
		//		User user=null;		//这么写是不可取 
		Connection conn = ConnectDb.Connect();
		QueryRunner runner=new QueryRunner();//Date
		User result =runner.insert(conn, "insert into user " +
				"(username,password,realname,sex,province,city,certtype,cert,birthday,usertype,content,rule,status,loginip,imagepath) " +
				"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new BeanHandler<User>(User.class),user.getUsername(),
				Md5Utils.md5(user.getPassword()),user.getRealname(),user.getSex(),user.getProvince(),user.getCity(),
				user.getCertType(),user.getCert(),user.getBirthday(),
				user.getUserType(),user.getContent(),user.getRule(),user.getStatus(),user.getLoginip(),user.getImagepath());
		return result;
		
		
	}			
}
